package Classes;

import java.util.ArrayList;

import android.util.Log;

import Classes.MapSpaceExtends.MapSpace;
import Classes.PersonExtends.Hero;
import Classes.PersonExtends.Person;
import Utils.EnumDirect;

public class Scene {
	
	SceneMap map;
	ArrayList<Person> alPerson;
	Hero hero;
	
	public Scene(SceneMap map,ArrayList<Person> alPerson,Hero hero){
		
		this.map=map;
		this.alPerson=alPerson;
		this.hero=hero;
	}

	public boolean checkCanGo(int detaX, int detaY) {
		// TODO Auto-generated method stub
		int nextX=hero.x+detaX;
		int nextY=hero.y+detaY;
		
		if(nextX<0||nextY<0||nextX>=map.width||nextY>=map.height){
			return false;
		}		

		
		if(IsCanPass(nextX,nextY)==false){
			return false;
		}

		if(checkHaveNpc(nextX,nextY)==true){
			return false;
		}
		
		return true;
	}

	private boolean IsCanPass(int nextX, int nextY) {
		// TODO Auto-generated method stub

		MapSpace space=map.mapGuid[nextX][nextY];
		if(space==null){
			return true;
		}
		return space.canPass();
	}

	private boolean checkHaveNpc(int X, int Y) {
		// TODO Auto-generated method stub
		for(Person p:alPerson){
			if(p.x==X&&p.y==Y){
				return true;
			}
		}
		return false;
	}
	
	private Person getNpc(int X, int Y){
		for(Person p:alPerson){
			if(p.x==X&&p.y==Y){
				return p;
			}
		}
		return null;
	}

	public void heroGo(int detaX, int detaY) {
		// TODO Auto-generated method stub
		hero.Go(detaX, detaY);
		
	}

	public void heroToDo() {
		// TODO Auto-generated method stub

		int heroFrontX=hero.x;
		int heroFrontY=hero.y;
		if(hero.direct==EnumDirect.NORTH){
			heroFrontY--;
		}else if(hero.direct==EnumDirect.SOUTH){
			heroFrontY++;
		}else if(hero.direct==EnumDirect.EAST){
			heroFrontX++;
		}else if(hero.direct==EnumDirect.WEST){
			heroFrontX--;
		}
		if(checkHaveNpc(heroFrontX,heroFrontY)){
			Person p=getNpc(heroFrontX,heroFrontY);
			p.Talking(hero.direct);
		}else{
		 MapSpace ms=	map.mapGuid[hero.x][hero.y];
		 if(ms!=null){
			 ms.forCheckDo();
		 }
		}
		
		
		
	}

	public void setHeroDirect(EnumDirect direct) {
		// TODO Auto-generated method stub
		hero.direct=direct;
	}

	
}
